package org.binglan.youziyuepai.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.apache.tomcat.jni.OS;
import org.binglan.youziyuepai.entity.UserContactWay;
import org.binglan.youziyuepai.enums.code.ContactTypeEnum;
import org.binglan.youziyuepai.mapper.UserContactWayMapper;
import org.binglan.youziyuepai.service.UserContactWayService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.binglan.youziyuepai.utils.EnumUtils;
import org.binglan.youziyuepai.vo.UsetContactWayVo;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 用户表 服务实现类
 * </p>
 *
 * @author sanjin
 * @since 2019-04-09
 */
@Service
public class UserContactWayServiceImpl extends ServiceImpl<UserContactWayMapper, UserContactWay> implements UserContactWayService {

    @Transactional(propagation = Propagation.REQUIRED)
    @Override
    public boolean saveOrUpdateUserContactWay(String userId, Integer contactType, String contactMark) {


        // 查找该条记录是否存在
        UserContactWay queryContactWay = this.queryContactWayByUserIdAndContactType(userId, contactType);
        if (queryContactWay == null) {
            // 记录不存在，进行添加
            UserContactWay userContactWay = new UserContactWay();
            userContactWay.setUserId(userId);
            userContactWay.setContactType(contactType);
            userContactWay.setContactMark(contactMark);
            this.save(userContactWay);
        } else {
            // 记录存在，进行修改
            queryContactWay.setContactMark(contactMark);
            this.updateById(queryContactWay);
        }
        return true;
    }

    @Transactional(propagation = Propagation.SUPPORTS)
    @Override
    public List<UserContactWay> queryAllUserContactWayByUserId(String userId) {
        UserContactWay userContactWay = new UserContactWay();
        userContactWay.setUserId(userId);
        return this.list(new QueryWrapper<>(userContactWay));
    }

    @Transactional(propagation = Propagation.SUPPORTS)
    @Override
    public UserContactWay queryContactWayByUserIdAndContactType(String userId, Integer contactType) {
        UserContactWay userContactWay = new UserContactWay();
        userContactWay.setUserId(userId);
        userContactWay.setContactType(contactType);
        return getOne(new QueryWrapper<>(userContactWay));
    }

    @Transactional(propagation = Propagation.SUPPORTS)
    @Override
    public List<UsetContactWayVo> queryUserContactWayVoByUserId(String userId) {
        UserContactWay query = new UserContactWay();
        query.setUserId(userId);
        List<UserContactWay> list = this.list(new QueryWrapper<>(query));
        List<UsetContactWayVo> resultVos = new ArrayList<>(list.size());
        list.forEach(item -> {
            UsetContactWayVo vo = new UsetContactWayVo();
            vo.setId(item.getId());
            vo.setContactType(item.getContactType());
            vo.setContactName(EnumUtils.getMessageByCode(item.getContactType(), ContactTypeEnum.class));
            vo.setContactMark(item.getContactMark());
            resultVos.add(vo);
        });

        return resultVos;
    }


}
